package class4;

/**
 * 返回大于n的最小的2的幂
 */
public class Code10_NearPowerOf2 {
    public static void main(String[] args) {
        int n = 3;
        int i = nearPowerOf2(n);
        System.out.println("i = " + i);
    }

    public static int nearPowerOf2(int n) {
        if (n <= 0) {
            return 1;
        }
        n--;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return n + 1;
    }
}
